<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Chapter;
use app\models\Classes;
use app\models\Comment;
use app\models\Course;
use yii;


class CourseLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getCourse($course_type,$query = [], $page = 1, $limit = 10){
        $course = Course::find()->filterWhere($query)->andWhere("course_type=$course_type")->andWhere("is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $course->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $course->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'course/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getVideo($query = [], $page = 1, $limit = 10){
        $video = Chapter::find()->filterWhere($query)->andWhere("course_id=1")->andWhere("is_del=0")->orderBy('chapter_order')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $video->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $video->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'course/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getClass($query = [], $page = 1, $limit = 10){
        $class = Classes::find()->filterWhere($query)->andWhere("is_del=0")->orderBy('class_order')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $class->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $class->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'course/class',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addCourse($params,$upload){
        $course = new Course();
        $course->course_name = $params['course_name'];
        $course->course_pic = $upload;
        if(isset($params['course_price']))
            $course->course_price = $params['course_price'];
        else
            $course->course_price = 0.01;
        $course->course_desc = $params['course_desc'];
        $course->course_type = $params['course_type'];
        if(isset($params['extend_type']) && $params['extend_type'])
            $course->extend_type = $params['extend_type'];
        $course->add_time = date('Y-m-d H:i:s');
        return $course->save();
    }

    public function getById($id)
    {
        return Course::find()->where(['course_id' => $id])->asArray()->one();
    }

    public function saveStatus($id)
    {
        $course = Course::findOne($id);
        /*$courses = Course::findAll(['course_type'=>$course->course_type]);
        foreach($courses AS $value){
            $value->course_status = 0;
            $value->save();
        }*/
        if($course->course_status)
            $course->course_status = 0;
        else
            $course->course_status = 1;
        return $course->save();
    }

    public function delete($id){
        if(!$course = Course::findOne($id)) return false;
        $course->is_del = 1;
        return $course->save();
        /*return $course->delete();*/
    }

    public function getComment($course_id,$query = [], $page = 1, $limit = 10){
        $comment = Comment::find()
            ->select('co.course_name,u.nick_name,u.mobile,c.*')
            ->from('{{%comment}} c')
            ->leftJoin('{{%course}} co','co.course_id=c.course_id')
            ->leftJoin('{{%user}} u','u.user_id=c.user_id')
            ->filterWhere($query)->andWhere("c.course_id=$course_id")->andWhere("c.is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $comment->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $comment->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'comment/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getCommentByCourseType($course_type,$query = [], $page = 1, $limit = 10){
        $comment = Comment::find()
            ->select('co.course_name,u.nick_name,u.mobile,c.*')
            ->from('{{%comment}} c')
            ->leftJoin('{{%course}} co','co.course_id=c.course_id')
            ->leftJoin('{{%user}} u','u.user_id=c.user_id')
            ->filterWhere($query)->andWhere("co.course_type=$course_type")->andWhere("c.is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $comment->asArray()->all();
        if($course_type==1){
            $url = 'course/comment-taotao';
        }elseif($course_type==2){
            $url = 'course/comment-yct';
        }elseif($course_type==3){
            $url = 'course/comment-hsk';
        }elseif($course_type==4){
            $url = 'course/comment-pronunciation';
        }elseif($course_type==5){
            $url = 'course/comment-children';
        }elseif($course_type==6){
            $url = 'course/comment-dele';
        }elseif($course_type==7){
            $url = 'course/comment-naughty';
        }
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $comment->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => $url,
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getCommentById($id){
        return Comment::find()
            ->select('co.course_type,c.*')
            ->from('{{%comment}} c')
            ->leftJoin('{{%course}} co','co.course_id=c.course_id')
            ->where(['comment_id'=>$id])->asArray()->one();
    }

    public function deleteComment($id){
        if(!$comment = Comment::findOne($id)) return false;
        $comment->is_del =1;
        return $comment->save();
        /*return $comment->delete();*/
    }

    public function addClass($params){
        $class = new Classes();
        $class->class_name = $params['class_name'];
        $class->paper_id = $params['paper_id'];
        $class->add_time = date('Y-m-d H:i:s');
        $class->class_order = $params['class_order'];
        return $class->save();
    }

    public function getClassById($id){
        return Classes::find()->where(['class_id'=>$id])->asArray()->one();
    }

    public function editClassById($id, $params){
        if(!$class = Classes::findOne($id)) return false;
        $class->class_name = $params['class_name'];
        $class->paper_id = $params['paper_id'];
        $class->class_order = $params['class_order'];
        return $class->save();
    }

    public function deleteClass($id){
        if(!$class = Classes::findOne($id)) return false;
        $class->is_del = 1;
        return $class->save();
        /*return $class->delete();*/
    }

    public function getChapter($course_id,$query=[],$page=1,$limit=10){

        $chapter = Chapter::find()
            ->filterWhere($query)->andWhere("course_id=$course_id")->andWhere("is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $chapter->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $chapter->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'chapter/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getChapterByCourse($course_id,$query=[],$page=1,$limit=10){
        if(isset($query['course_id'])){
            $course_id = $query['course_id'];
        }else{
            $query['course_id'] = $course_id;
        }
        $chapter = Chapter::find()
            ->filterWhere($query)->andWhere("course_id=$course_id")->andWhere("is_del=0")->orderBy('chapter_order')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $chapter->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $chapter->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'course/edit',
                'query' => $query
            ]
        ];
        return $return;
    }



    public function editById($id, $params, $uploadresult){
        if(!$course = Course::findOne($id)) return false;
        $course->course_name = $params['course_name'];
        if($uploadresult)
            $course->course_pic = $uploadresult;
        $course->course_price = $params['course_price'];
        $course->course_desc = $params['course_desc'];
        if(isset($params['extend_type']) && $params['extend_type'])
            $course->extend_type = $params['extend_type'];
        return $course->save();
    }

    public function addChapter($params,$uploadresult,$uploadresult1,$uploadresult2){
        $chapter = new Chapter();
        $chapter->chapter_no = $params['chapter_no'];
        $chapter->chapter_name = $params['chapter_name'];
        $chapter->chapter_pic = $uploadresult;
        $chapter->chapter_video = $uploadresult1;
        $chapter->chapter_test_video = $uploadresult2;
        $chapter->video_time = $params['video_time'];
        $chapter->paper_id = $params['paper_id'];
        $chapter->course_id = $params['course_id'];
        $chapter->add_time = date('Y-m-d');
        $chapter->chapter_order = $params['chapter_order'];
        return $chapter->save();
    }

    public function addVideo($params,$uploadresult,$uploadresult1,$uploadresult2){
        $chapter = new Chapter();
        $chapter->chapter_name = $params['chapter_name'];
        $chapter->chapter_pic = $uploadresult;
        $chapter->chapter_video = $uploadresult1;
        $chapter->chapter_test_video = $uploadresult2;
        $chapter->video_time = $params['video_time'];
        $chapter->chapter_price = $params['chapter_price'];
        $chapter->course_id = 1;
        $chapter->add_time = date('Y-m-d');
        $chapter->chapter_order = $params['chapter_order'];
        return $chapter->save();
    }

    public function getChapterById($id){
        return Chapter::find()->where(['chapter_id'=>$id])->asArray()->one();
    }

    public function deleteChapter($id){
        if(!$chapter = Chapter::findOne($id)) return false;
        $chapter->is_del = 1;
        return $chapter->save();
        /*return $chapter->delete();*/
    }

    public function editChapter($id,$params,$uploadresult,$uploadresult1,$uploadresult2){
        if(!$chapter = Chapter::findOne($id)) return false;
        $chapter->chapter_no = $params['chapter_no'];
        $chapter->chapter_name = $params['chapter_name'];
        if($uploadresult)
            $chapter->chapter_pic = $uploadresult;
        if($uploadresult1)
            $chapter->chapter_video = $uploadresult1;
        if($uploadresult2)
            $chapter->chapter_test_video = $uploadresult2;
        $chapter->video_time = $params['video_time'];
        $chapter->paper_id = $params['paper_id'];
        $chapter->chapter_order = $params['chapter_order'];
        return $chapter->save();
    }

    public function editVideo($id,$params,$uploadresult,$uploadresult1,$uploadresult2){
        if(!$chapter = Chapter::findOne($id)) return false;
        $chapter->chapter_name = $params['chapter_name'];
        if($uploadresult)
            $chapter->chapter_pic = $uploadresult;
        if($uploadresult1)
            $chapter->chapter_video = $uploadresult1;
        if($uploadresult2)
            $chapter->chapter_test_video = $uploadresult2;
        $chapter->video_time = $params['video_time'];
        $chapter->chapter_price = $params['chapter_price'];
        $chapter->chapter_order = $params['chapter_order'];
        return $chapter->save();
    }

}
